<?php 
include TMPL_PATH.'public/header.phtml';
?>
<div class="subnav">
    <div class="content-menu ib-a blue line-x">
        <a href='<?php echo U('product/index');?>'><em>商品列表</em></a><span>|</span>
        <a href='javascript:;' class="on" ><em>商品批量修改</em></a>
    </div>
</div>
<div class="pad_10">
<div class="common-form">
<form name="theForm" action="__SELF__" method="post" id="theForm" onsubmit="return getGoodsIDs()">
<div id="price-div">
<table width="100%" cellspacing="1" cellpadding="3" class="table_form contentWrap">
  <tr>
    <th width="200" class="narrow-label">选择商品的方式：</th>
    <td>
      <label><input name="select_method" id="sm_cat" type="radio" value="cat" checked onclick="toggleSelectMethod(this.value)">根据商品分类、品牌</label>
      <label><input name="select_method" id="sm_sn" type="radio" value="sn" onclick="toggleSelectMethod(this.value)">根据商品货号</label>    </td>
  </tr>
  <tr id="cat_1">
    <th class="narrow-label" id="cat_2">选择商品分类：</th>
    <td id="cat_3">&nbsp;<select name="cat" id="cat" onchange="getGoods()">
      <option value="0" selected>请选择...</option>
      <?php echo $categorySelectList;?>
      </select></td>
  </tr>
  <tr id="cat_7" style="display:none">
    <th class="narrow-label" id="cat_8">选择商品品牌：</th>
    <td id="cat_9">&nbsp;<select name="brand" id="brand" onchange="getGoods()">
      <option value="0" selected>请选择...</option>
      <?php if($brandList):foreach ($brandList as $brand):?>
      <option value="<?php echo $brand['id']?>"><?php echo $brand['name']?></option>
      <?php endforeach;endif;?>
      </select></td>
  </tr>
  <tr id="cat_4">
    <th class="narrow-label" id="cat_5">商品列表：</th>
    <td valign="middle" id="cat_6">
      <table  border="0" cellspacing="1" cellpadding="3">
      <tr>
        <td>待选列表：</td>
        <td align="center" valign="middle">&nbsp;</td>
        <td>选定列表：</td>
      </tr>
      <tr>
        <td width="45%">
          <select name="srcList" size="10" multiple id="srcList" style="width: 100%" ondblclick="addGoods()"></select>
        </td>
        <td align="center" valign="middle"><input name="add" type="button" class="button" id="add" value="&gt;&gt;" onclick="addGoods()" /><br />
          <input name="del" class="button" type="button" id="del" value="&lt;&lt;" onclick="delGoods()" /></td>
        <td width="45%">
          <select name="destList" size="10" multiple id="destList" style="width: 100%" ondblclick="delGoods()"></select>
        </td>
      </tr>
      </table>
    </td>
  </tr>
  <tr style="display:none" id="sn_1">
    <th class="narrow-label" style="display:none" id="sn_2">输入商品货号：<br />（每行一个）</th>
    <td style="display:none" id="sn_3"><textarea name="sn_list" rows="10" cols="40" id="sn_list"></textarea></td>
  </tr>
  <tr>
    <th class="narrow-label">编辑方式：</th>
    <td>
      <label><input name="edit_method" type="radio" value="each" checked>
      逐个编辑</label>
      <label><input type="radio" name="edit_method" value="all">统一编辑</label>    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input name="dosubmit" type="submit" value="进入编辑" class="button" id="dosubmit" />
      <input type="hidden" name="goods_ids" value="" /></td>
  </tr>
  </table>
</div>
    <div class="bk15"></div>
</form>
</div>
</div>
</body>
</html>
<script language="javascript">
<!--

var ele = document.forms['theForm'].elements;
/**
 * 切换选择商品方式：
 * @param: method 当前方式 cat sn
 */
function toggleSelectMethod(method)
{
  if (method == 'cat')
  {
      var catDisplay = '';
      var snDisplay = 'none';
  }
  else
  {
      var catDisplay = 'none';
      var snDisplay = '';
  }

  for (var i = 1; i <= 9; i++)
  {
     document.getElementById('cat_' + i).style.display = catDisplay;
  }
  for (var i = 1; i <= 3; i++)
  {
     document.getElementById('sn_' + i).style.display = snDisplay;
  }
}

/**
 * 取得商品
 */
function getGoods()
{
    var catId   = ele['cat'].value;
    var brandId = ele['brand'].value;
    if (catId > 0 || brandId > 0)
    {
        $.getJSON('<?php echo U('product/goods_batch_ajax');?>',{is_ajax:1,cat_id:catId,brand_id:brandId}, getGoodsResponse);
    }
    else
    {
        ele['srcList'].options.length = 0;
    }
}

function getGoodsResponse(result)
{
  if (result.error == 0)
  {
    ele['srcList'].options.length = 0;

    for (var i = 0; i < result.content.length; i++)
    {
      var opt = document.createElement('OPTION');
      opt.value = result.content[i].id;
      opt.text = result.content[i].name;
      ele['srcList'].options.add(opt);
    }
  }else{
			$('#srcList').html('');
	  }
}

/**
 * 添加商品
 */
function addGoods()
{
    var src = document.getElementById('srcList');
    var dest = document.getElementById('destList');

    for (var i = 0; i < src.options.length; i++)
    {
        if (src.options[i].selected)
        {
            var exist = false;
            for (var j = 0; j < dest.options.length; j++)
            {
                if (dest.options[j].value == src.options[i].value)
                {
                    exist = true;
                    break;
                }
            }
            if (!exist)
            {
                var opt = document.createElement('OPTION');
                opt.value = src.options[i].value;
                opt.text = src.options[i].text;
                dest.options.add(opt);
            }
        }
    }
}

/**
 * 删除商品
 */
function delGoods()
{
    var dest = document.getElementById('destList');

    for (var i = dest.options.length - 1; i >= 0 ; i--)
    {
        if (dest.options[i].selected)
        {
            dest.options[i] = null;
        }
    }
}

/**
 * 取得选择的商品id，赋值给隐藏变量。同时检查是否选择或输入了商品
 */
function getGoodsIDs()
{
    if (document.getElementById('sm_cat').checked)
    {
        var idArr = new Array();
        var dest = document.getElementById('destList');
        for (var i = 0; i < dest.options.length; i++)
        {
            idArr.push(dest.options[i].value);
        }
        if (idArr.length <= 0)
        {
            alert('请选择商品');
            return false;
        }
        else
        {
            document.forms['theForm'].elements['goods_ids'].value = idArr.join(',');
            return true;
        }
    }
    else
    {
        if (document.forms['theForm'].elements['sn_list'].value == '')
        {
            alert('请输入产品编号');
            return false;
        }
        else
        {
            return true;
        }
    }
}
//-->
</script>